package com.example.rain.ttdt3;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;

import java.io.IOException;

public class MediaService extends Service {
    private int currentTime = 0;
    private int totalTime;
    private boolean isPause = false;
    private MediaPlayer mediaPlayer;
    private boolean isPlay= false;

    public MediaService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mediaPlayer = new MediaPlayer();

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        int action = intent.getIntExtra("action", -1);
        switch (action) {
            case MainActivity.PLAY:
                if (isPause){
                    isPause=false;
                    synchronized (this) {
                        this.notify();
                        mediaPlayer.start();
                    }
                }
                else {
                    play();
                }
                break;
            case MainActivity.PAUSE:
                pause();
                break;
        }
        return super.onStartCommand(intent, flags, startId);
    }

    public class MyBinder extends Binder {
        public  int getCurrentTime(){
            return currentTime;
        }
        public  int getTotalTime(){
            return totalTime;
        }
        public boolean isPlay(){
            if(mediaPlayer.isPlaying()) {
                isPlay = true;
            }
            return  isPlay;
        }
        public int seekTo(int currentPosition){
            isPause=false;

            mediaPlayer.seekTo(currentPosition);
            mediaPlayer.start();


            synchronized (MediaService.this) {
                MediaService.this.notify();

            }
            return currentPosition;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }
    public void pause() {
        isPause=true;
        mediaPlayer.pause();

    }

    public void play() {
        mediaPlayer.reset();
        try {
            mediaPlayer.setDataSource("/sdcard/Music/b.mp3");
            mediaPlayer.prepare();
            totalTime = mediaPlayer.getDuration();
            mediaPlayer.start();

            new Thread() {

                @Override
                public void run() {
                    synchronized (MediaService.this) {
                        for (int i = currentTime; i < totalTime; i++) {
                            if (isPause) {
                                try {
                                    MediaService.this.wait();
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                            try {
                                Thread.sleep(20);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            currentTime = mediaPlayer.getCurrentPosition();
                        }
                    }
                }
            }.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
